package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.entity.Player;
import com.xcity.db.entity.Ranking;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.player.cache.PlayerCacheService;

import naga.x.App;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class RankingListResponse extends Response {
//	"data":{"roles":[{},
	
	public Data data;
	
	public RankingListResponse(int serial, byte type, int rank, List<Ranking> players, int off, int len) {
		super(serial);
		data = new Data();
		if (players != null && !players.isEmpty()) {
			if (off + len > players.size()) {
				len = players.size() - off;
			}
			data.roles = new ArrayList<Role>(len);
			PlayerCacheService as = App.getApp().getServiceManager().get(PlayerCacheService.class);
			for (int i = off, to = off + len; i < to; i++) {
				Ranking ranking = players.get(i);
				Player actor = as.find(ranking.getPlayerId());
				Role role = new Role();
				data.roles.add(role);
				role.roleName = actor.getName();
				role.roleId = actor.getId();
				role.lv = actor.getLevel();
				role.exp = actor.getExp();
				role.img = actor.getIcon();
				role.totalGold = actor.getFixedAssets();
				role.gold = actor.getGold();
				role.allGold = role.totalGold + role.gold;
				role.province = actor.getProvince();
				role.charm = actor.getPool().getIntValue(PlayerPool.PROPERTY_CHARM);
				role.robNum = actor.getPool().getIntValue(PlayerPool.PROPERTY_SUM_OF_BUILD_ATTACK);
				role.vipLvl = actor.getVipLvl();
				role.rownum = ranking.getIndex() + 1;//i + 1;
				role.value = ranking.getValue();
			}
		}
		data.type = type;
		data.rank = rank;
	}
	
	public static class Data {
		public byte type;
		public int rank;
		public List<Role> roles = Collections.emptyList();
	}
	
	public static class Role {
		public String roleName;
		public long roleId;
		public int lv;
		@JSONField(name = "EXP")
		public long exp;
		public String img;
		public long totalGold; 
		public long gold;
		public long allGold;
		public String province;
		@Deprecated
		public long lastLoginTime; // no use
		public int vipLvl; // vip等级
		public int charm; // 魅力值
		public int robNum; // 掠夺次数
		public long value;
		public int rownum;
	}
	
}
